package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.AreaService;
import co.gov.medellin.ssti.sub.model.service.ProgramaService;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.AreaVO;
import co.gov.medellin.ssti.sub.model.vo.ProgramaVO;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;

public class ModificarSubprogramaAction extends Action {
	@Autowired
	AreaService areaServiceImpl;
	@Autowired
	ProgramaService programaServiceImpl;
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		String strCodeSub = request.getParameter("Codigo");
		if (strCodeSub == null)
			strCodeSub = request.getParameter("Subprograma");

		String strCodeProg = request.getParameter("code");

		SubprogramaVO objSubProgVO = new SubprogramaVO();
		ProgramaVO objProgVO = new ProgramaVO();

		Collection<SubprogramaVO> coBeneficio = null;
		Collection<AreaVO> coSubSecret = null;
		Collection<ProgramaVO> coPrograma = null;
		try {
			if (strCodeSub != null && !strCodeSub.equals("")) {
				objSubProgVO.setCode(new Long((strCodeSub)));
				coBeneficio = subprogramaServiceImpl
						.findInformacionSubprogramaByCriteria(objSubProgVO);

				coPrograma = programaServiceImpl
						.findAllProgramaByCriteria(objProgVO);

				if (strCodeProg == null) {
					objSubProgVO = null;
					objSubProgVO = new SubprogramaVO();
					List<SubprogramaVO> lBeneficio = new LinkedList<SubprogramaVO>();
					lBeneficio = (List<SubprogramaVO>) coBeneficio;
					objSubProgVO = (SubprogramaVO) lBeneficio.get(0);
					strCodeProg = String
							.valueOf(objSubProgVO.getCodePrograma());
				}

				AreaVO objAreaVO = new AreaVO();
				objAreaVO = areaServiceImpl.findAreaByProg(strCodeProg);

				coSubSecret = areaServiceImpl.findAllSubAreas(String
						.valueOf(objAreaVO.getCode()));

				request.setAttribute("coBeneficio", coBeneficio);
				request.setAttribute("coSubSecret", coSubSecret);
				request.setAttribute("secretaria", objAreaVO.getNombre());
				request.setAttribute("programas", coPrograma);
				request.setAttribute(
						"paraEmpresa",
						(coBeneficio != null && !coBeneficio.isEmpty()) ? coBeneficio
								.iterator().next().getCampoA4()
								: null);
				if (strCodeProg != null)
					request.setAttribute("codeProg", strCodeProg);
			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
		}

		return mapping.findForward("success");
	}
}